package commonclass;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liweimin
 * @date 2021/3/27 11:26
 * @description java比较器
 * @since
 */
public class CompareTest {
    class Good implements Comparable<Good> {
        private String name;
        private Integer count;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }

        public Good(String name, Integer count) {
            this.name = name;
            this.count = count;
        }

        @Override
        public String toString() {
            return "Good{" +
                    "name='" + name + '\'' +
                    ", count=" + count +
                    '}';
        }

        @Override
        public int compareTo(Good good) {
            if (this.count > good.count) {
                return 1;
            } else if (this.count < good.count) {
                return -1;
            } else {
                return 0;
            }
        }
    }

    @Test
    public void test() {

        Good nike = new Good("Nike", 122);
        Good lining = new Good("LiNing", 14);
        Good kappa = new Good("Kappa", 34);
        System.out.println(nike.compareTo(kappa));
        List<Good> goods = new ArrayList<>();
        goods.add(nike);
        goods.add(kappa);
        goods.add(lining);
        System.out.println(goods);
        Collections.sort(goods);
        System.out.println(goods);
    }

    @Test
    public void test1() {
        Good nike = new Good("Nike", 122);
        Good lining = new Good("LiNing", 14);
        Good kappa = new Good("Kappa", 34);
        System.out.println(nike.compareTo(kappa));
        List<Good> goods = new ArrayList<>();
        goods.add(nike);
        goods.add(kappa);
        goods.add(lining);
        System.out.println(goods);
        Collections.sort(goods, ((o1, o2) -> {
            return o1.getName().compareTo(o2.getName());
        }));
        System.out.println(goods);
    }
}
